<?
class LikeAction{
	
	public $smarty;
	public $name;
    public $like_person_openid; // 点赞人
    public $bei_like_openid; // 被点赞人
    public $js_sdk;
    private $model;

	//构造方法，初始化 
	public function __construct(&$_smarty,&$_js_sdk,&$_model) {
		$this->smarty = $_smarty;
        $this->js_sdk = $_js_sdk;
        $this->model = $_model;
	}

    /**
     * 显示页面
     */
	public function action(){
        if(!empty($this->name)){
            $this->smarty->assign('name',$this->name);
        }
        if(!empty($this->like_person_openid)){ // 点赞人
            $this->smarty->assign('like_person_openid',$this->like_person_openid);
        }
        if(!empty($this->bei_like_openid)){ // 被点赞人
            $this->smarty->assign('bei_like_openid',$this->bei_like_openid);
        }
        // 用户点赞排行列表
        $this->smarty->assign('userList', $this->model->getUserOrderByLikeCount());
        if(!empty($this->js_sdk)){
            $signPackage = $this->js_sdk->GetSignPackage();
            $this->smarty->assign('appId',$signPackage["appId"]);
            $this->smarty->assign('timestamp',$signPackage["timestamp"]);
            $this->smarty->assign('nonceStr',$signPackage["nonceStr"]);
            $this->smarty->assign('signature',$signPackage["signature"]);
        }
		$this->smarty->display('templates/like.tpl');
	}

    /**
     * 用户注册的方法
     * @param $name
     * @param $phone
     * @param $openid
     */
    public function existsOpenid(&$openid){
        $this->model->openid = $openid;
        return $this->model->checkUserToPhoneOrOpenid();
    }

    /**
     * 获取一个用户
     * @param $id
     * @return mixeds
     */
    public function getOneUser(&$id){
        $this->model->id = $id;
        return $this->model->getOneUser();

    }

    /**
     * 分享
     */
    public  function  addLike(&$like_person_openid,&$bei_like_openid){
        if($this->model->existsLike($like_person_openid,$bei_like_openid)){
            echo 'no_已投票过了';
        }else{
            if($this->model->addLike($like_person_openid,$bei_like_openid)){
                $like_count = $this->model->getLikeCountToOpenid($bei_like_openid);
                $like_count += 1;
                $this->model->like_count = $like_count;
                $this->model->openid = $bei_like_openid;
                if($this->model->updateLikeCountToOpenid()){
                    echo 'ok_投票成功';
                }else{
                    echo 'no_投票失败';
                }
            }else{
                echo 'no_投票失败,请重新投票';
            }
        }
        exit;
    }
	
}